/*
 * spa.model.article.js
 * 기사 관련 모델.
 *
 */

spa.model.article = function() {
  // Module variables start
  var configMap = {
  },
  stateMap = {
    $json : null,
    $article : null,
    category_now : null,
    title_now : null,
    page_now : null
  },
  jqueryMap = {
  },
  initModule, load, getJSON, getList, getArticle,
  getNowCategory, getNowTitle, setNowTitle, setNowCategory,
  getNowArticleMap, setNowArticleMap;
  // Module variables end
  
  // public methods start
  
  // /initModule/ start
  initModule = function() {
  };
  // /initModule/ end
  
  // /getJSON/ start
  getJSON = function() {
    return stateMap.$json;
  };
  // /getJSON/ end
  
  // /getList/ Start
  getList = function(page, category, title, start_date, end_date) {
    var jsonList,
        request = $.ajax({
          type: "POST",
          url: "jsp/ArticleListLoad.jsp",
          data: {
            "page" : page,
            "category" : category,
            "title" : title,
            "start_date" : start_date,
            "end_date" : end_date
          },
          dataType : "json"
        });

    request.success(function(response) {
      setNowArticleMap(response);
      setNowCategory( category );
      setNowTitle( title );
      setNowPage( ( page ) ? page : 1 );
      spa.view.renderArticleTile(response);
      spa.view.renderArticlePagination(category, title, ( page ) ? page : 1 );
    });
  }
  // /getList/ End
  // /getArticle/ Start
  getArticle = function( id ) {
    var article,
        request = $.ajax({
          type: "POST",
          url: "jsp/ArticleLoad.jsp",
          data: {
            "article_num" : id
          },
          dataType : "json"
        });
    request.success(function(response) {
      stateMap.$article = response;
    });
    return request;
  };
  // /getArticle/ End
  // /getNowCategory/ Start
  getNowCategory = function() {
    return stateMap.category_now;
  };
  // /getNowCategory/ End
  // /getNowTitle/ Start
  getNowTitle = function() {
    return stateMap.title_now;
  };
  // /getNowTitle/ End
  setNowCategory = function(category) {
    stateMap.category_now = category;
  };
  setNowTitle = function(title) {
    stateMap.title_now = title;
  };
  getNowArticleMap = function() {
    return stateMap.$json;
  };
  setNowArticleMap = function( arti_map ) {
    stateMap.$json = arti_map;
  };
  getNowPage = function() {
    return stateMap.page_now;
  }
  setNowPage = function( page ) {
    stateMap.page_now = page;
  }
  // public methods end

  return {
    initModule : initModule,
    getJSON : getJSON,
    getList : getList,
    getArticle : getArticle,
    getNowCategory : getNowCategory,
    getNowTitle : getNowTitle,
    setNowCategory : setNowCategory,
    setNowTitle : setNowTitle,
    getNowArticleMap : getNowArticleMap,
    setNowArticleMap : setNowArticleMap,
    getNowPage : getNowPage,
    setNowPage : setNowPage
  };
}();
